﻿using System.Data;
using Channelvn.Cached;
using Core.Library;

namespace DataObject.Photo
{
    public class PhotoCache
    {
        public static DataTable Photos_GetByMemberID(int id, int round)
        {
            var listParams = new object[] { id, round };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Photos_GetByMemberID", listParams);
            var fromcache = CacheController.GetCache(Constants.PHOTO_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = PhotoController.Photos_GetByMemberID(id, round))
                {
                    CacheController.Add(Constants.PHOTO_CACHE_CATEGORY, cacheName, dt, Constants._5min);
                    return dt;
                }
            }
        }

        public static int Photos_CountByMemberID(int id, int round)
        {
            var listParams = new object[] { id, round };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Photos_CountByMemberID", listParams);
            var fromcache = CacheController.GetCache(Constants.PHOTO_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (int)fromcache;
            }
            else
            {
                int r = PhotoController.Photos_CountByMemberID(id, round);
                CacheController.Add(Constants.PHOTO_CACHE_CATEGORY, cacheName, r, Constants._5min);
                return r;
            }
        }

        public static void Photo_RemoveCacheMember(int id, int round)
        {
            var listParams = new object[] { id, round };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Photos_GetByMemberID", listParams);
            CacheController.Remove(Constants.PHOTO_CACHE_CATEGORY, cacheName);
            cacheName = CacheHelper.CacheKey.GetCacheKey("Photos_CountByMemberID", listParams);
            CacheController.Remove(Constants.PHOTO_CACHE_CATEGORY, cacheName);
        }

        public static DataTable Photos_ListAllPhotoPaging(int round, int pageIndex, int pageSize)
        {
            var listParams = new object[] { round, pageIndex, pageSize };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("Photos_ListAllPhotoPaging", listParams);
            var fromcache = CacheController.GetCache(Constants.PHOTO_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = PhotoController.Photos_ListAllPhotoPaging(round, pageIndex, pageSize))
                {
                    CacheController.Add(Constants.PHOTO_CACHE_CATEGORY, cacheName, dt, Constants._10min);
                    return dt;
                }
            }
        }
    }
}
